<?php
class Page_ajax extends V_View {
        function check () {
            if (isset ($_POST['check'])) {
                if ($_POST['check']=='company_name') {
                    if (isset ($_POST['str']) and trim($_POST['str'])!='') {
                        $count=V_DB::count('v_work_list','client_company_name='.$_POST['str']);
                        if ($count==0) echo "OK";
                        else {
                        	$list=V_DB::fetch('SELECT id FROM v_work_list WHERE client_company_name='.$_POST['str']);
                        	if (isset($_POST['id']) and $_POST['id']==$list['id']) echo "OK";
                        	else echo "EXIST";
                        }
                    } else echo "OK";
                }
                if ($_POST['check']=='company_phone') {
                    if (isset ($_POST['str']) and trim($_POST['str'])!='') {
                        $count=V_DB::count('v_work_list','client_company_phone='.$_POST['str']);
                        if ($count==0) echo "OK";
                        else {
                        	$list=V_DB::fetch('SELECT id FROM v_work_list WHERE client_company_phone='.$_POST['str']);
                        	if (isset($_POST['id']) and $_POST['id']==$list['id']) echo "OK";
                        	else echo "EXIST";
                        }
                    } else echo "OK";
                }
            }
        }
        function change () {
            if ($_POST['change']=='state') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                    if (isset ($_POST['state']) and $_POST['state']!='') {
                        $query = "UPDATE v_work_list_states SET state_id=".V_DB::escape($_POST['state']).
                                 " WHERE list_id=".V_DB::escape($_POST['id']);
                        V_DB::exec($query);
                        echo "OK";
                    }
                }
            }
        	if ($_POST['change']=='calldate') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                    if (isset ($_POST['date']) and $_POST['date']!='') {
                        $query = 'UPDATE v_work_list_states SET state_date="'.V_DB::escape($_POST['date']).'"'.
                                    " WHERE list_id=".V_DB::escape($_POST['id']);
                        V_DB::exec($query);
                        echo "OK";
                    }
                }
            }
        	if ($_POST['change']=='list_comment') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                    if (isset ($_POST['comment'])) {
                        $query = 'UPDATE v_work_list_states SET state_comment="'.V_DB::escape($_POST['comment']).'"'.
                                    " WHERE list_id=".V_DB::escape($_POST['id']);
                        V_DB::exec($query);
                        echo "OK";
                    }
                }
            }
            if ($_POST['change']=='work_get') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                    if (isset ($_POST['user_id']) and $_POST['user_id']==V_User::getId()) {
                        $query = "UPDATE v_work_list SET user_work_id=".V_DB::escape($_POST['user_id']).
                                    ", user_work_isget=1".
                                    " WHERE id=".V_DB::escape($_POST['id']);
                        V_DB::exec($query);
                        echo "OK";
                    }
                }
            }
        	if ($_POST['change']=='unwork') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                	$query = "UPDATE v_work_list SET user_work_id=0".
                             ", user_work_isget=0".
                             " WHERE id=".V_DB::escape($_POST['id']);
                    V_DB::exec($query);
                    echo "OK";
                }
            }
            if ($_POST['change']=='add_send') {
                if (isset ($_POST['id']) and $_POST['id']!='') {
                    $query = "UPDATE v_work_list SET user_add_issend=1".
                                " WHERE id=".V_DB::escape($_POST['id']);
                    V_DB::exec($query);
                    echo "OK";
                }
            }
        }
    }